// pages/community/community.js
var app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    isChosen: false,
    page: 1,
    leader: {
      id: 0,
      leader_name: '',
      wx_avatar_url: '',
      community_name: '',
      address: '',
    },
    searchKw: '',
    isBack: 0,
    leadList: [],
    location: {
      lng: '',
      lat: ''
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    if (options.is_back == 1) {
      that.setData({
        isBack: options.is_back,
      })
    }
  },

  onShow: function () {
    this.getCmdList()
    //this.getLocation()
  },

  /**
   * 上拉
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading()
    this.setData({
      searchKw: ''
    })
    this.getCmdList()
    wx.hideNavigationBarLoading()
    wx.stopPullDownRefresh()
  },

  /**
   * 
   */
  onReachBottom: function () {
    let that = this,
      page = that.data.page
    page = page + 1
    that.setData({
      page: page
    })
    that.getCmdList()
  },

  /**
   * 获取团长列表
   */
  getCmdList: function () {
    let that = this,
      list = that.data.leadList,
      data, k, j
    wx.showLoading({
      mask: true,
      title: app.globalData.loadText,
    })
    wx.getLocation({
      success: res => {
        that.setData({
          'location.lat': res.latitude,
          'location.lng': res.longitude
        })
        app.getAccessToken(app.getPageUrl(getCurrentPages())).then(function (accessToken) {
          wx.request({
            url: app.globalData.hostAPI + '/api/v1/rgl-cmdr/list?access_token=' + accessToken,
            data: {
              page: that.data.page,
              lat: res.latitude,
              lng: res.longitude
            },
            success: res => {
              data = res.data.data
              if (res.data.status === 1) {
                if (data.selected.id != '') {
                  that.setData({
                    isChosen: true,
                    'leader.id': data.selected.id,
                    'leader.leader_name': data.selected.leader_name,
                    'leader.wx_avatar_url': data.selected.wx_avatar_url,
                    'leader.community_name': data.selected.community_name,
                    'leader.address': data.selected.address,
                  })
                  if (that.data.isBack != 1) {
                    that.setData({
                      'wapp.leaderData': that.data.leader
                    })
                    wx.switchTab({
                      url: '../index/index',
                    })
                  }
                }
                if (that.data.page == 1) {
                  that.setData({
                    leadList: data.list
                  })
                } else {
                  if (data.list) {
                    for (k in data.list) {
                      list.push(data.list[k])
                    }
                    that.setData({
                      leadList: list
                    })
                  } else {
                    wx.showToast({
                      icon: 'none',
                      title: '无更多数据',
                    })
                  }
                }
              }
              wx.hideLoading()
            }
          })
        })
      },
    })
    // app.getAccessToken(app.getPageUrl(getCurrentPages())).then(function (accessToken) {
    //   wx.request({
    //     url: app.globalData.hostAPI + '/api/v1/rgl-cmdr/list?access_token=' + accessToken, 
    //     data: {
    //       page: that.data.page
    //     },
    //     success: res => {
    //       data = res.data.data
    //       if(res.data.status === 1) {
    //         if(data.selected.id != '') {
    //           that.setData({
    //             isChosen: true,
    //             'leader.id': data.selected.id,
    //             'leader.leader_name': data.selected.leader_name,
    //             'leader.wx_avatar_url': data.selected.wx_avatar_url,
    //             'leader.community_name': data.selected.community_name,
    //             'leader.address': data.selected.address,
    //           })
    //           if (that.data.isBack != 1) {
    //             that.setData({
    //               'wapp.leaderData': that.data.leader
    //             })
    //             wx.switchTab({
    //               url: '../index/index',
    //             })
    //           }
    //         }
    //         if(that.data.page == 1) {
    //           that.setData({
    //             leadList: data.list
    //           })
    //         } else {
    //           if(data.list) {
    //             for (k in data.list) {
    //               list.push(data.list[k])
    //             }
    //             that.setData({
    //               leadList: list
    //             })
    //           } else {
    //             wx.showToast({
    //               icon: 'none',
    //               title: '无更多数据',
    //             })
    //           }
    //         } 
    //       }
    //       wx.hideLoading()
    //     }
    //   })
    // })
  },

  /**
   * choiceLeader
   */
  choiceLeader: function (e) {
    let that = this,
      id = e.currentTarget.id,
      data
    wx.showLoading({
      mask: true,
      title: app.globalData.loadText,
    })
    app.getAccessToken(app.getPageUrl(getCurrentPages())).then(function (accessToken) {
      wx.request({
        url: app.globalData.hostAPI + '/api/v1/rgl-cmdr/choice?access_token=' + accessToken,
        data: {
          id: id
        },
        success: res => {
          data = res.data.data
          if (res.data.status === 1) {
            that.setData({
              'wapp.leaderData': that.data.leader
            })
            wx.hideLoading()
            wx.switchTab({
              url: '../index/index',
            })
          }
        }
      })
    })
  },

  /**
   * cnameValue
   */
  cnameValue: function (e) {
    this.setData({
      searchKw: e.detail.value
    })
  },

  /**
   * search
   */
  search: function () {
    let that = this
    wx.showLoading({
      mask: true,
      title: app.globalData.loadText,
    })
    that.setData({
      page: 1
    })
    app.getAccessToken(app.getPageUrl(getCurrentPages())).then(function (accessToken) {
      wx.request({
        url: app.globalData.hostAPI + '/api/v1/rgl-cmdr/list?access_token=' + accessToken,
        data: {
          page: that.data.page,
          search: that.data.searchKw,
          lat: that.data.location.lat,
          lng: that.data.location.lng
        },
        success: res => {
          if (res.data.status === 1) {
            if (res.data.data.list) {
              that.setData({
                leadList: res.data.data.list
              })
            } else {
              that.setData({
                leadList: []
              })
            }
            wx.hideLoading()
          }
        }
      })
    })
  }
})